package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.entity.Article;
import com.hoongs.v1.service.ArticleService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/article")
@Validated
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result save(Article article) {
        return articleService.save(article);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Article article, BindingResult bindingResult, @RequestHeader("token") String token) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : articleService.add(article, token);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return articleService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Article article) {
        return articleService.update(article);
    }

    @GetMapping("/selectById")
    public Result selectById(String id) {
        return articleService.selectById(id);
    }

    @GetMapping("/selectPageList")
    public Result selectPageList(Integer page, Integer size, String title, String tag, String category, Integer status, String[] times) {
        return articleService.selectPageList(page, size, title, tag, category, status, times);
    }

}
